class Calender {
  constructor(options = {}){
    const defaultParames = {
      weeksFormat :  ['SUN','MON','TUE','WED','THU','FRI','SAT'],
      monthsFormat : ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
    }
    this._options = Object.assign({}, defaultParames, options)
    const today = new Date()
    this.todayTamp = Date.now()
    // 日
    this.nowDate = today.getDate()
    this.nowMonth = today.getMonth()
    this.nowYear = today.getFullYear()
    this.currentMonth = today.getMonth()
    this.currentYear = today.getFullYear()
    this.currentDate = today.getDate()
    // 日历缓存收集
    this.CalendarStorageCollect = {}
  }
  changeStamp2DateObj(number){
    return new Date(number)
  }
  changeParames2DateObj(year, month, date){
    return new Date(year, month, date)
  }
  /* 输出日历所需的 星期、月、日 */
  outputTheMessage( _date ){
    // call new Date()
    return {
      weekDay: this._options.weeksFormat[_date.getDay()],
      month : this._options.monthsFormat[_date.getMonth()],
      date: _date.getDate() ,
      dateStr : _date.getFullYear() + '-' + ( _date.getMonth() + 1 ) + '-' + _date.getDate()
    }
  }
  buildCalendarItems(year, month){
    // 先从 日历store 里面找, 找不到在生成
    if( `${year}/${month}` in this.CalendarStorageCollect ) return this.CalendarStorageCollect[`${year}/${month}`]
    let items = []
    const theMonthLenght = this.changeParames2DateObj(year, month+1, 0 ).getDate()
    console.log('month length  ', theMonthLenght)
    for( let i =1; i < theMonthLenght+1; i++ ){
      const dayItem = this.changeParames2DateObj(year, month, i )
      items.push(this.outputTheMessage(dayItem))
    }
    // collection for use again
    this.CalendarStorageCollect[`${year}/${month}`] = items
    return items
  }
  initData(){
    const months =
      [ ...this.getPreMonth(),
        ...this.buildCalendarItems(this.nowYear, this.nowMonth, this.nowDate),
        ...this.getNextMonth()
      ]
    /*[
      ...this.buildCalendarItems(this.nowYear, this.nowMonth, this.nowDate),

    ]*/
    const currentDateStr = this.nowYear + '-' + (  this.nowMonth + 1 ) + '-' + this.nowDate
    return {months ,currentDateStr }
  }
  scrollToLeft(){
    const month = this.getPreMonth()

    if(this.currentMonth == 1){
      this.currentMonth = 12
      this.currentYear -= 1
    }
    else{
      this.currentMonth -= 1
    }
    return month
  }
  scrollToRight(){
    const month = this.getNextMonth()
    if(this.currentMonth == 12){
      this.currentMonth = 1
      this.currentYear += 1
    }
    else{
      this.currentMonth += 1
    }
    return month
  }

  // 上月
  getPreMonth(){
    if(this.currentMonth == 1){
      return this.buildCalendarItems( this.currentYear - 1, 12 )
    }
    else{
      return this.buildCalendarItems( this.nowYear, this.currentMonth - 1 )
    }
  }
  // 下月
  getNextMonth(){
    if(this.currentMonth == 12){
      return this.buildCalendarItems( this.currentYear +1 , 1 )
    }
    else{
      return this.buildCalendarItems( this.nowYear, this.currentMonth +1 )
    }
  }



}

export default Calender
